<template>
    <div class="content">
        <el-row :gutter="40">
            <el-col :span="8" v-for="(value,index) in roomList" :key="index" >
                <el-card :body-style="{ padding: '0px' }">
                    <img src="@/assets/logo.png" class="image">
                    <div >
                        <span><i class="black"></i> 对战房间{{value.id}} <i class="white"></i></span>
                        <div class="bottom clearfix">
                            <el-button size="small" round type="danger" @click="joinRoom(value.id)" icon="el-icon-trophy"  :disabled="value.people_sum>=2?true:false" >人数：{{value.people_sum}}{{value.people_sum>=2?"房间爆满":"加入"}}</el-button>
                        </div>
                    </div>
                </el-card>
            </el-col>
        </el-row>
        <div class="mask" v-if="maskShow">
            <div class="outer-ring">
                <div class="inner-circle">
                    <i class="black"></i>
                    <i class="white"></i>
                    <span class="text">匹配中...</span>
                </div>
                <el-tooltip class="item" effect="dark" content="点击取消匹配" placement="bottom-start">
                    <i class="mask-close el-icon-close" @click="unmatch()"></i>
                </el-tooltip>
            </div>
        </div>
    </div>
</template>

<script>
    import {initWebSocket,websocketsend,sendWebsocket} from "@/uitls/websocket.js"

    export default {
        name: "RoomList",
        data:function () {
            return {
                roomList:[],
                roomId:0,
                maskShow:false,
            }
        },
        created() {
            this.checkUser();
        },
        methods: {
            unmatch(){
                var name = localStorage.getItem("name");
                var data={
                    "message_type":10003,//退出匹配
                    "content":String(this.roomId),
                    "sender" :name,
                };
                console.log(data)
                websocketsend(data);
                this.maskShow=false;
            },
            joinRoom(id){
                var name = localStorage.getItem("name");
                var data={
                  "message_type":10002,
                  "content":String(id),
                  "sender" :name,
                };
                console.log(data);
                websocketsend(data);
                this.roomId=id;
                localStorage.setItem("room_id",id);
                this.maskShow=true;
            },
            wsMessage(redata){
                console.log("消息接收",redata);
                var message_type=redata.message_type;
                var content=redata.content;
                var parse = JSON.parse(content);
                switch (message_type) {
                    case 10001:
                        this.roomList=parse;
                        break;
                    case 10004:
                        this.$router.push({name:"bang"});
                        break;
                    case 10006:
                        this.$router.push({name:"bang"});
                        break;
                    case 20001:
                        this.$message({
                            message: parse.content,
                            type: 'warning'
                        });
                        break;
                    default:
                }
            },
            checkUser(){
                this.$axios.get(process.env.VUE_APP_HTTP_URL+"check")
                    .then((response) => {
                        var data=response.data;
                        var code=data.code;
                        if (code===10009){
                            this.open();
                        }
                        initWebSocket();
                        sendWebsocket(this.wsMessage);
                }).catch((response)=>{
                    console.log(response)
                })
            },

            open() {
                this.$prompt('请输入呢称', '', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type:"info",
                    center:true,
                    inputPattern: /^[a-z]{2,10}$/,
                    inputErrorMessage: '昵称不正确，请输入小写英文，不超过2-10位的英文昵称'
                }).then(({ value }) => {
                this.$axios.post(process.env.VUE_APP_HTTP_URL+"user/register",{
                    name:value
                }).then((response) => {
                    console.log(response);
                    if (response.data.code===10007){
                        this.$message({
                            type: 'success',
                            message: '欢迎你: ' + value
                        });
                        localStorage.setItem("name",value);
                        initWebSocket();
                        sendWebsocket(this.wsMessage);
                    }else {
                        this.$message({
                            type: 'waring',
                            message: response.data.msg
                        });
                        this.open();
                    }

                    console.log(response.data,"成功")
                }).catch((response)=>{
                    console.log(response);
                    this.$message({
                        type: 'error',
                        message: '出现未知错误'
                    });
                    this.open();
                })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消输入'
                    });
                });
            }
        }
    }
</script>

<style scoped>
    .content{
        box-sizing: border-box;
        padding: 0 20%;
    }
    .el-card{
        margin-bottom: 20px;
        height: 300px;
        background-color: #d1cf9e;
    }
    .el-card>.image{
        height: 50%;
    }

    .bottom {
        margin-top: 13px;
        line-height: 12px;
    }
    .image {
        width: 100%;
        display: block;
    }

    .clearfix:before,
    .clearfix:after {
        display: table;
        content: "";
    }

    .clearfix:after {
        clear: both
    }
    .black,.white{
        border-radius: 50%;
        display: inline-block;
        width: 15px;
        height: 15px;
        vertical-align: middle;
    }
    .black{
        background-color: black;
    }
    .white{
        background-color: white;
    }

    .mask{
        position: fixed;
        z-index: 2000;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
    }
    .outer-ring{
        position: absolute;
        z-index: 3000;
        width: 300px;
        height: 300px;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        border:1px solid #f6b950;
        border-radius: 50%;
        background-color: #FAD961;
        background-image: linear-gradient(135deg, #FAD961 0%, #F76B1C 100%);

    }
    .inner-circle{
        position: absolute;
        width: 150px;
        height: 150px;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        border-radius: 50%;
        border:1px solid #fff;
    }
    .inner-circle>.text{
        position: absolute;
        white-space:nowrap;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
    }
    .inner-circle>.black,.inner-circle>.white{
        width: 40px;
        height: 40px;
        border-radius: 50%;
    }
    .inner-circle>.black{
        position: absolute;
        left: 0;
    }
    .inner-circle>.white{
        position: absolute;
        bottom: 0;
        right: 0;
    }
    .mask::after{
        content: "";
        position: fixed;
        display: block;
        width: 100%;
        height: 100%;
        background-color: rgba(0,0,0,0.7);
    }
    .mask-close{
        color: white;
        width: 30px;
        height: 30px;
        font-size: 30px;
        position: absolute;
        right: 0;
    }

</style>